package com.event.management.backend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.event.management.backend.domain.QuestionnaireAnswer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 问卷答案Mapper接口
 */
@Mapper
public interface QuestionnaireAnswerMapper extends BaseMapper<QuestionnaireAnswer> {
    
    /**
     * 根据活动ID查询问卷提交记录（分页）
     * @param page 分页参数
     * @param eventId 活动ID
     * @return 问卷提交记录
     */
    @Select("SELECT a.* FROM questionnaire_answers a WHERE a.event_id = #{eventId} ORDER BY a.submitted_at DESC")
    IPage<QuestionnaireAnswer> findAnswersByEventId(Page<QuestionnaireAnswer> page, @Param("eventId") Long eventId);
    
    /**
     * 获取活动问卷提交数量
     * @param eventId 活动ID
     * @return 提交数量
     */
    @Select("SELECT COUNT(1) FROM questionnaire_answers WHERE event_id = #{eventId}")
    int countAnswersByEventId(@Param("eventId") Long eventId);
    
    /**
     * 获取总问卷提交数量
     * @return 提交数量
     */
    @Select("SELECT COUNT(1) FROM questionnaire_answers")
    Integer countTotalSubmissions();
    
    /**
     * 根据问卷提交ID查询答案列表
     * @param submissionId 问卷提交ID
     * @return 答案列表
     */
    @Select("SELECT * FROM questionnaire_answers WHERE submission_id = #{submissionId}")
    List<QuestionnaireAnswer> findBySubmissionId(@Param("submissionId") Long submissionId);
    
    /**
     * 统计用户提交的问卷数量
     * @param userId 用户ID
     * @return 提交数量
     */
    @Select("SELECT COUNT(DISTINCT id) FROM questionnaire_answers WHERE user_id = #{userId}")
    Integer countUserSubmissions(@Param("userId") Integer userId);
} 